<test>
    <create_query>
        create table matview_1
        (
            a String,
            b_count AggregateFunction(uniq, UInt64)
        ) Engine=MergeTree partition by tuple()
        ORDER by a
        SETTINGS index_granularity = 1024;
    </create_query>

    <create_query>
        create table matview_10000
        (
            a String,
            b_count AggregateFunction(uniq, String)
        ) Engine=MergeTree partition by tuple()
        ORDER by a
        SETTINGS index_granularity = 1024;
    </create_query>


    <drop_query>DROP TABLE IF EXISTS matview_1</drop_query>
    <drop_query>DROP TABLE IF EXISTS matview_10000</drop_query>

    <fill_query>
        INSERT INTO matview_10000
        SELECT a, uniqState(b) b_count
        FROM
        (
            SELECT toString(intDiv(number, 20000)) a, toString(number % 10000) b
            FROM numbers_mt(20000000)
        )
        GROUP BY a
        SETTINGS max_insert_threads=8;
    </fill_query>
    <fill_query>OPTIMIZE TABLE matview_10000 FINAL</fill_query>

    <fill_query>
        INSERT INTO matview_1
        SELECT '1', uniqState(number) b_count
        FROM
        (
            SELECT *
            FROM numbers_mt(2000000)
        )
        GROUP BY number
        SETTINGS max_insert_threads=8;
    </fill_query>
    <fill_query>OPTIMIZE TABLE matview_1 FINAL</fill_query>

    <!-- Test with ~10000 elements per state -->
    <query>select a, uniqMerge(b_count) as b_count from matview_10000 prewhere a='55' group by a FORMAT Null SETTINGS max_threads=1;</query>
    <query>select uniqMerge(b_count) as b_count from matview_10000 FORMAT Null SETTINGS max_threads=1;</query>

    <!-- Test with ~1 elements per state -->
    <query>select uniqMerge(b_count) as b_count FROM matview_1 FORMAT Null SETTINGS max_threads=1;</query>
</test>
